package gui.commander;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import hypeerweb.HyPeerWeb;
import hypeerweb.Node;

import gui.Main.GUI;
import gui.mapper.NodeListing;
import gui.newWindows.BroadcastWindow;
import gui.newWindows.SetUpWindow;
import gui.newWindows.SendWindow;

/**
 * Standard Commands are a basic set of commands that are needed to test a
 * HyPeerWeb. A open command line is available which may allow for more specific
 * commands as is appropriate.
 * 
 * @author Matthew Smith
 * 
 * @domain Buttons - Actions taken on the HyPeerWeb
 * @domain Command Bar - Use of embedded functions to affect the HyPeerWeb
 * 
 */
public class StandardCommands extends JPanel
{
    /* Root of the GUI */
    private GUI main;

    /* Container for tha Command Field and Execute Button */
    private JPanel fieldPanel;

    /* Command Field for inputed Commands */
    // private JTextField commandField;

    /* Button to execute a command in the command Field */
    // private JButton executeButton;

    /* Conatainer for all basic command buttons */
    private JPanel buttonPanel;

    /* Basic buttons commands */
    private JButton insertNode, removeNode, sendNode, broadcastNode,
            setUpHypeerWebNode,killNode;

    /* List nodes to display as targets */
    private Object[] nodeList;

    SendWindow sendWindow = null;
    BroadcastWindow broadcastWindow = null;
    SetUpWindow setUpWindow = null;

    /**
     * Creates and intailizes the panel of basic commands as well as gathers a
     * list of nodes
     * 
     * @param main
     *            - root of the GUI
     */
    public StandardCommands(GUI main)
    {
        this.main = main;

        init();

        updateList();
    }

    public SendWindow getSendWindow()
    {
        return sendWindow;
    }

    public SetUpWindow getSetUpdWindow()
    {
        return setUpWindow;
    }

    public BroadcastWindow getBroadcastWindow()
    {
        return broadcastWindow;
    }

    public void setSendWindowToNull()
    {
        sendWindow = null;
    }

    public void setSetUpWindowToNull()
    {
        setUpWindow = null;
    }

    public void setBroadcastWindowToNull()
    {
        broadcastWindow = null;
    }

    /**
     * Initializes and sets up the GUI objects of the Class
     */
    public void init()
    {
        this.setLayout(new BorderLayout());

        // Build the open command Area
        fieldPanel = new JPanel(new BorderLayout());

        // Build the Basic Button Area
        buttonPanel = new JPanel(new GridLayout(1, 4));

        // Build the insert button
        insertNode = new JButton("(+) Insert Node");
        insertNode.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                insertButtonPressed();
            }
        });
        insertNode.setMnemonic(KeyEvent.VK_ADD);
        buttonPanel.add(insertNode);

        // Build the remove button
        removeNode = new JButton("(-) Delete Node");
        removeNode.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                removeButtonPressed();
            }
        });
        removeNode.setMnemonic(KeyEvent.VK_SUBTRACT);
        buttonPanel.add(removeNode);

        // Build the send button
        sendNode = new JButton("Send Message");
        sendNode.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                sendButtonPressed();
            }
        });
        sendNode.setMnemonic(KeyEvent.VK_S);
        buttonPanel.add(sendNode);

        // Build the broadcast button
        broadcastNode = new JButton("Broadcast");
        broadcastNode.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                broadcastButtonPressed();
            }
        });
        broadcastNode.setMnemonic(KeyEvent.VK_B);
        buttonPanel.add(broadcastNode);

        // Build the setup button
        setUpHypeerWebNode = new JButton("Construct");
        setUpHypeerWebNode.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                setUpButtonPressed();
            }
        });
        setUpHypeerWebNode.setMnemonic(KeyEvent.VK_U);
        buttonPanel.add(setUpHypeerWebNode);
        
        killNode= new JButton("kill");
       killNode.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                killButtonPressed();
               
            }
        });
        killNode.setMnemonic(KeyEvent.VK_K);
        buttonPanel.add(killNode);

        add(buttonPanel, BorderLayout.SOUTH);
    }

    /**
     * Gathers information of the nodes in the HyPeerWeb and stores them in a
     * list of Nodes
     */
    public void updateList()
    {
        nodeList = new Object[30];
        for (int i = 0; i < 30; i++)
        {
            nodeList[i] = Integer.toBinaryString(i);
        }
    }
    public void killButtonPressed()
    {
        GUI.getSingleton(null).getHyPeerWeb().kill();
        try
        {
            Thread.sleep(250);
        }
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        GUI.getSingleton().getHyPeerWeb().deleteUpdateGui();
    }
    /**
     * Inserts a Node into the HyPeerWeb
     */
    public void insertButtonPressed()
    {
        if (main.getHyPeerWebDebugger().getMapper().getNodeListing().listSize() >= main
                .getHyPeerWebDebugger().getMapper().getNodeListing().MAX_NUMBER_OF_NODES)
        {
            main.getHyPeerWebDebugger().getStatus()
                    .setContent("Error, too many nodes.");
        }
        else
        {
            int startId = main.getHyPeerWebDebugger().getMapper().getNodeListing().getSelectedIndex();
            
            if(GUI.getSingleton(null).getHyPeerWeb().isHyPeerWebEmpty())
            {
                GUI.getSingleton(null).getHyPeerWeb().addToHyPeerWeb(-1);
            }
            else if(GUI.getSingleton(null).getHyPeerWeb().size() == 0)
            {
                System.out.println("hypeerweb has nodes, but I don't");
                GUI.getSingleton(null).getHyPeerWeb().addToHyPeerWeb(0);
            }
            else if(main.getHyPeerWeb().getNode(startId) != null)
            {
                GUI.getSingleton(null).getHyPeerWeb().addToHyPeerWeb(startId);
            }
            else
            {
                main.getHyPeerWebDebugger().getStatus().setContent("Error, start node not found.");
            }
        }
    }
    /**
     * Removes a node from the HyPeerWeb
     */
    public void removeButtonPressed()
    {
        System.out.println("remove button pressed");

        if (main.getHyPeerWebDebugger().getMapper().getNodeListing().listSize() <= 1)
        {
            main.getHyPeerWebDebugger().getStatus().setContent("Error, too few nodes.");
        }
        else
        {
            int startId = main.getHyPeerWebDebugger().getMapper().getNodeListing().getSelectedIndex();
            System.out.println("startId " + startId);

            if (GUI.getSingleton(null).getHyPeerWeb().getNode(startId) != null)
            {
                GUI.getSingleton(null).getHyPeerWeb().removeFromHyPeerWeb(startId);
            }
            else
            {
                main.getHyPeerWebDebugger().getStatus().setContent("Error, node to delete not found.");
            }
        }
    }

    /*
     * setup button pressed
     */
    public void setUpButtonPressed()
    {

        setUpWindow = new SetUpWindow(main, "Set Up");

    }

    /**
     * Sends a message through the HyPeerWeb
     */
    public void sendButtonPressed()
    {
        sendWindow = new SendWindow(main, "Send Message");
    }

    /**
     * Broadcasts a message through the HyPeerWeb
     */
    public void broadcastButtonPressed()
    {
        broadcastWindow = new BroadcastWindow(main, "Broadcast Message");
    }

}
